//
//  YZAutoCellFactory.m
//  VET
//
//  Created by Lam BG on 2018/7/13.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZAutoCellFactory.h"

// auto cell id enum
#import "YZAutoFormHeader.h"
#import "YZDatePickView.h"

// auto model
#import "YZAutoVCModel.h"
#import "YZAutoHFModel.h"
#import "YZAutoRowModel.h"
#import "YZAutoItemModel.h"
#import "YZAutoSectionModel.h"

// 第三方
#import "MJExtension.h"

#import "NSArray+YZCategory.h"
#import "NSString+DisplayTime.h"
#import "NSDate+YZDatePickView.h"

@implementation YZAutoCellFactory

SingletonM

- (void)registerAutoFormCells:(UITableView*)tableView {
    
    // 注册 form cell
    [tableView registerNib:[UINib nibWithNibName:@"YZAutoTVCell" bundle:nil] forCellReuseIdentifier:kAutoTVCellID];
    [tableView registerNib:[UINib nibWithNibName:@"YZAutoJumpCell" bundle:nil] forCellReuseIdentifier:kJumpCellID];
    [tableView registerNib:[UINib nibWithNibName:@"YZAutoInputCell" bundle:nil] forCellReuseIdentifier:kAutoInputCellID];
    [tableView registerNib:[UINib nibWithNibName:@"YZAutoTitleCell" bundle:nil] forCellReuseIdentifier:kAutoTitleTVCellID];
}

- (void)registerAutoItemCells:(UITableView*)tableView {
    
    // 注册 item cell
    [tableView registerNib:[UINib nibWithNibName:@"YZAutoItemCell" bundle:nil] forCellReuseIdentifier:kAutoItemCellID];
}

- (UITableViewCell*)generateCell:(UITableView*)tableView withRowModel:(YZAutoRowModel*)rowModel {
    
    if (rowModel.type == AutoCellTypeJumpVC ||
        rowModel.type == AutoCellTypeActionSheet ||
        rowModel.type == AutoCellTypeSelectSheet ||
        rowModel.type == AutoCellTypeJumpTime ||
        rowModel.type == AutoCellTypeSelectPigVC ||
        rowModel.type == AutoCellTypeSelectPigsVC) {
        
        // 弹出效果的 cell
        return [tableView dequeueReusableCellWithIdentifier:kJumpCellID forIndexPath:rowModel.indexPath];
        
    } else if (rowModel.type == AutoCellTypeInput) {
        
        // 直接输入
        return [tableView dequeueReusableCellWithIdentifier:kAutoInputCellID forIndexPath:rowModel.indexPath];
        
    } else if (rowModel.type == AutoCellTypeInputTV) {
        
        // 输入范围
        return [tableView dequeueReusableCellWithIdentifier:kAutoTVCellID forIndexPath:rowModel.indexPath];
        
    } else if (rowModel.type == AutoCellTypeTitleTV) {
        
        // 输入范围
        return [tableView dequeueReusableCellWithIdentifier:kAutoTitleTVCellID forIndexPath:rowModel.indexPath];
    }
    
    
    // 选择 item cell
    else if (rowModel.type == AutoCellTypeSelectItem) {
        
        return [tableView dequeueReusableCellWithIdentifier:kAutoItemCellID forIndexPath:rowModel.indexPath];
    }
    
    
    
    
    
    // 如果返回 nil 检查配置 json
    else {
        
        NSLog(@"如果返回 nil 检查配置 json");
        return nil;
    }
}

#pragma mark - 默认值

- (void)sutupRowModelDefaultValue:(YZAutoRowModel*)rowModel {

    if (rowModel.type == AutoCellTypeJumpTime) {
        
        // 默认值
        NSString *defaultValue = @"";
        // 截止日期默认值
        if ([@"taskDeadline" isEqualToString:rowModel.paramKey]) {
            
            NSUInteger timeInterval = 0;
            if (rowModel.displayValue.length == 0) {
                
                // 获取当前时间 按规则显示
                NSDate *date = [NSDate date];
                NSDate *nextDay = [NSDate dateWithTimeInterval:24 * 60 * 60 sinceDate:date];
                timeInterval = [nextDay timeIntervalSince1970];
                // 设置服务器值 时间戳
                rowModel.paramValue = [@(timeInterval) stringValue];
                // 按日期格式
                defaultValue = [NSString timeStringFromFormat:@"MM月dd日 HH:mm" withDate:nextDay];
                
            } else {
                
                timeInterval = [rowModel.paramValue doubleValue];
                NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
                // 按日期格式
                defaultValue = [NSString timeStringFromFormat:@"MM月dd日 HH:mm" withDate:date];
            }
            
            rowModel.displayValue = defaultValue;
        }
        
        // 出生日期 和 创建日期
        if ([@"herdBirthdate" isEqualToString:rowModel.paramKey] ||
            [@"herdCreatedate" isEqualToString:rowModel.paramKey] ||
            [@"finishedDate" isEqualToString:rowModel.paramKey] ||
            [@"transferDate" isEqualToString:rowModel.paramKey] ||
            [@"dieoutTime" isEqualToString:rowModel.paramKey] ||
            [@"swineBirthdate" isEqualToString:rowModel.paramKey]) {
            
            NSUInteger timeInterval = 0;
            if (rowModel.displayValue.length == 0) {
                
                // 获取当前时间 按规则显示
                NSDate *date = [NSDate date];
                timeInterval = [date timeIntervalSince1970];
                // 设置服务器值 时间戳
                rowModel.paramValue = [@(timeInterval) stringValue];
                // 按日期格式
                defaultValue = [NSString timeStringFromFormat:@"yyyy-MM-dd" withDate:date];
            } else {
                
                timeInterval = [rowModel.paramValue doubleValue];
                NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
                // 按日期格式
                defaultValue = [NSString timeStringFromFormat:@"yyyy-MM-dd" withDate:date];
            }
            
            rowModel.displayValue = defaultValue;
        }
        
        // 免疫时间
        if ([@"vaccinationTime" isEqualToString:rowModel.paramKey] ||
            [@"diagnosisTime" isEqualToString:rowModel.paramKey]) {
            
            NSUInteger timeInterval = 0;
            if (rowModel.displayValue.length == 0) {
                
                // 获取当前时间 按规则显示
                NSDate *date = [NSDate date];
                timeInterval = [date timeIntervalSince1970];
                // 设置服务器值 时间戳
                rowModel.paramValue = [@(timeInterval) stringValue];
                // 按日期格式
                defaultValue = [NSString timeStringFromFormat:@"yyyy-MM-dd HH:mm" withDate:date];
            } else {
                
                timeInterval = [rowModel.paramValue doubleValue];
                NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
                // 按日期格式
                defaultValue = [NSString timeStringFromFormat:@"yyyy-MM-dd HH:mm" withDate:date];
            }
            
            rowModel.displayValue = defaultValue;
        }
    }
}

#pragma mark - 时间相关方法

- (NSDate*)popMinDate:(YZAutoRowModel*)rowModel {
    
    // 截止日期默认值
    if ([@"taskDeadline" isEqualToString:rowModel.paramKey]) return [NSDate currentDayStartDate];
    
    return nil;
}

#pragma mark -

- (NSString*)showDateFormat:(YZAutoRowModel*)rowModel {
    
    // 截止日期默认值
    if ([@"taskDeadline" isEqualToString:rowModel.paramKey]) return @"MM月dd日 HH:mm";
    
    // 免疫时间
    if ([@"vaccinationTime" isEqualToString:rowModel.paramKey] ||
        [@"diagnosisTime" isEqualToString:rowModel.paramKey]) return @"yyyy-MM-dd HH:mm";
    
    return @"yyyy-MM-dd";
}


/** 需要一一对应 */
/** 显示日期格式 */
- (NSString*)showPickViewDateFormat:(YZAutoRowModel*)rowModel {
    
    // 截止日期默认值
    if ([@"taskDeadline" isEqualToString:rowModel.paramKey]) return @"yyyy-MM-dd HH:mm";
    
    // 免疫时间
    if ([@"vaccinationTime" isEqualToString:rowModel.paramKey] ||
        [@"diagnosisTime" isEqualToString:rowModel.paramKey]) return @"yyyy-MM-dd HH:mm";
    
    return @"yyyy-MM-dd";
}

/** 控件支持日期格式 */
- (YZDatePickerMode)showPickViewDatePickerMode:(YZAutoRowModel*)rowModel {
    
    // 截止日期默认值
    if ([@"taskDeadline" isEqualToString:rowModel.paramKey]) return YZDatePickerModeDateAndTime;
    
    // 免疫时间
    if ([@"vaccinationTime" isEqualToString:rowModel.paramKey] ||
        [@"diagnosisTime" isEqualToString:rowModel.paramKey]) return YZDatePickerModeDateAndTime;
    
    return YZDatePickerModeDate;
}
/** 需要一一对应 */

#pragma mark - 根据 item 生成 sections

- (void)sections:(NSMutableArray*)sections WithItems:(NSArray*)items andVCModel:(YZAutoVCModel*)vcModel {
    
    // 先成 item -> row model
    NSArray *data = [YZAutoItemModel mj_keyValuesArrayWithObjectArray:items];
    NSMutableArray *rows = [YZAutoRowModel mj_objectArrayWithKeyValuesArray:data];
    [rows setAllKeyValues:@{@"type" : @(AutoCellTypeSelectItem)}];
    
    if (vcModel.vcType == AutoVCTypeTaskAlarm) {
        
        // 在分配给 section
        YZAutoSectionModel *section1 = sections[0];
        section1.itemRows = [NSArray arrayWithObject:rows[0]];
        [rows removeObjectAtIndex:0];
        
        YZAutoSectionModel *section2 = sections[1];
        section2.itemRows = [NSArray arrayWithArray:rows];
        
    } else {
        
        // 默认在一组
        YZAutoSectionModel *section = sections[0];
        section.itemRows = [NSArray arrayWithArray:rows];
    }
}

@end
















